package com.aurora.oa.work.view;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.EditText;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.ToastUtil;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/20 0020.
 */
public class ApplyCommonActivity extends BaseActivity {

    @Bind(R.id.tv_title)
    EditText tvTitle;
    @Bind(R.id.tv_content)
    EditText tvContent;
    @Bind(R.id.tv_remark)
    EditText tvRemark;
    @Bind(R.id.bt_submit)
    Button btSubmit;
    ProgressDialog progressDialog;
    RequestHttpModel model = new RequestHttpModelImpl();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_apply_common);
        ButterKnife.bind(this);
        initTitlebar(true, "通用", true);
    }

    @OnClick(R.id.bt_submit)
    public void onClick() {
        String title =tvTitle.getText().toString().trim() ;
        String content=tvContent.getText().toString().trim();
        String remark =tvRemark.getText().toString().trim();
        if(title.equals("")){
            ToastUtil.show(this,"标题不能为空");
            return;
        }

        if(content.equals("")){
            ToastUtil.show(this,"内容不能为空");
            return;
        }
        progressDialog = ProgressDialog.show(this,null,"正在提交数据...");
        Map<String,String> params =new HashMap<>();
        params.put("title",title);
        params.put("content",content);
        params.put("remark",remark);
        model.postForm(AuroraConstants.SEND_COMMON_APPLY, params, new OnRequestHttpDataListener() {
            @Override
            public void onRequestDataSuccess(String body) {

                Gson gson = new Gson();
                HttpResult result = gson.fromJson(body, HttpResult.class);
                progressDialog.dismiss();
                if(result!=null&&result.success){
                    ToastUtil.show(getApplicationContext(),"提交成功!");
                    finish();
                }else{
                    ToastUtil.show(getApplicationContext(),result.msg);
                }
            }

            @Override
            public void onRequestDataFail(String message) {
                ToastUtil.show(getApplicationContext(),message);
                progressDialog.dismiss();
            }
        });
    }

}
